/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.gui;

import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.EventManager;
import it.polimi.se2.meteocal.control.NotificationManager;
import it.polimi.se2.meteocal.control.ParticipationManager;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.control.WeatherManager;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.EventNotification;
import it.polimi.se2.meteocal.entity.Participation;
import it.polimi.se2.meteocal.entity.ParticipationNotification;
import it.polimi.se2.meteocal.entity.Users;
import it.polimi.se2.meteocal.entity.Weather;
import it.polimi.se2.meteocal.helper.NotificationView;
import it.polimi.se2.meteocal.helper.ParticipationView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;



/**
 *
 * @author Ugo
 */
@ManagedBean
@ViewScoped
public class NotificationBean implements Serializable {
    @EJB
    private UserManager userManager;
    
    @EJB
    private CalendarManager calendarManager;
    
    @EJB
    private ParticipationManager participationManager;
    
    @EJB
    private NotificationManager notificationManager;
    
    @EJB
    private EventManager eventManager;
    
    @EJB
    private WeatherManager weatherManager;
    
    private List <ParticipationView> parteveinf=new ArrayList(); 
    private List <NotificationView> invitenotview=new ArrayList();
    private List <NotificationView> updatenotview=new ArrayList();
    private List <NotificationView> day1notview=new ArrayList();
    private List <NotificationView> days3notview=new ArrayList();
    private List <ParticipationNotification> partnotlist1;
    private List <ParticipationNotification> partnotlist2;
    private List <ParticipationNotification> partnotlist3;
    private List <EventNotification> eventnotlist=new ArrayList();
    
   
    private NotificationView select;
    private int i,j,l,m;
    private Event event;
    private Users userLogged;
    private Weather weather;
    
    private Date newdate;
    private Weather weather2; //quello relativo a newdate
    
    @PostConstruct
    public void init(){
        userLogged=userManager.getLoggedUser();
        partnotlist1= notificationManager.findParticipationNotificationByUser1(userLogged);
        if(partnotlist1!=null){
        for(ParticipationNotification p:partnotlist1){
            Event event= p.getParticipation().getEvent();
            Users user= calendarManager.findUserByCalendar(event.getCalendar().getId());
            invitenotview.add(new NotificationView(user.getEmail(),event.getTitle()));
        }
        }
        
        partnotlist2= notificationManager.findParticipationNotificationByUser2(userLogged);
        if(partnotlist2!=null){
        for(ParticipationNotification p:partnotlist2){
            Event event= p.getParticipation().getEvent();
            Users user= calendarManager.findUserByCalendar(event.getCalendar().getId());
            updatenotview.add(new NotificationView(user.getEmail(),event.getTitle()));
        }
        }
        
       partnotlist3= notificationManager.findParticipationNotificationByUser3(userLogged);
        if(partnotlist3!=null){
        for(ParticipationNotification p:partnotlist3){
            Event event= p.getParticipation().getEvent();
            Users user= calendarManager.findUserByCalendar(event.getCalendar().getId());
            day1notview.add(new NotificationView(user.getEmail(),event.getTitle()));
        }
        }
        
       List<Event> eventmycal= calendarManager.findEventsInCalendar(calendarManager.findCalendarByUser(userLogged));
       for(Event e:eventmycal){
           EventNotification eventn= notificationManager.findEventNotificationByEvent(e);
           if(eventn!=null){
               eventnotlist.add(eventn);
               days3notview.add(new NotificationView("Meteocal",eventn.getEvent().getTitle()));
           }
       }
          
    }
    
    public Event getEvent(){
        return this.event;
    }
    
    public void setEvent(Event event){
        this.event=event;
    }
    
    public Weather getWeather(){
        return this.weather;
    }
    
    public void setWeather(Weather weather){
        this.weather=weather;
    }
    
    public void loadPartEvent(){
       parteveinf.clear();
       i=0;
       for(NotificationView f:invitenotview){
           i++;
           if(f.equals(select)){
               event= partnotlist1.get(i-1).getParticipation().getEvent();
               
               break; 
           }  
       }  
       
       j=0;
       for(NotificationView f:updatenotview){
           j++;
           if(f.equals(select)){
               event= partnotlist2.get(j-1).getParticipation().getEvent();
               notificationManager.remove(partnotlist2.get(j-1));
               break; 
           }  
       }
       
         
       l=0;
       for(NotificationView f:day1notview){
           l++;
           if(f.equals(select)){
               event= partnotlist3.get(l-1).getParticipation().getEvent();
               notificationManager.remove(partnotlist3.get(l-1));
               break; 
           }  
       }
       
       m=0;
         for(NotificationView f:days3notview){
           m++;
           if(f.equals(select)){
               event= eventnotlist.get(m-1).getEvent();
               newdate=eventnotlist.get(m-1).getNewdate();
               weather2=weatherManager.getWeatherByLocationAndDate(event.getLocation(), newdate);
               notificationManager.remove(eventnotlist.get(m-1));
               break; 
           }  
       }
         
       weather=weatherManager.getWeatherByLocationAndDate(event.getLocation(), event.getData());
       List<Participation> partev;
       partev=participationManager.findParticipationByEvent(event);
       for(Participation p:partev)
            parteveinf.add(new ParticipationView(p.getUser().getName(),p.getUser().getSurname(),p.getUser().getEmail(),p.getState().toString()));
    }
    
    public List<ParticipationView> getParteveinf(){
        return this.parteveinf;
    }
    
    public void setParteveinf(List<ParticipationView> parteveinf) {
        this.parteveinf=parteveinf;
    }
    
     public List<NotificationView> getInvitenotview(){
        return this.invitenotview;
    }
    
    public void setInvitenotview(List<NotificationView> invitenotview) {
        this.invitenotview=invitenotview;
    }
    
    public List<NotificationView> getUpdatenotview(){
        return this.updatenotview;
    }
    
    public void setUpdatenotview(List<NotificationView> updatenotview) {
        this.updatenotview=updatenotview;
    }
    
    public List<NotificationView> getDay1notview(){
        return this.day1notview;
    }
    
    public void setDay1notview(List<NotificationView> day1notview) {
        this.day1notview=day1notview;
    }
    
    public List<NotificationView> getDays3notview(){
        return this.days3notview;
    }
    
    public void setDays3notview(List<NotificationView> days3notview) {
        this.days3notview=days3notview;
    }
    
    public NotificationView getSelect(){
        return this.select;
    }
    
    public void setSelect(NotificationView select){
        this.select=select;
    }
    
    public Date getNewdate(){
        return this.newdate;
    }
    
    public void setNewdate(Date newdate){
        this.newdate=newdate;
    }
    
    public Weather getWeather2(){
        return this.weather2;
    }
    
    public void setWeather2(Weather weather2){
        this.weather2=weather2;
    }
    
    public String reload(){
        return "/user/notification?faces-redirect=true";  
    }
    
    public String setRefused(){
        Participation part = partnotlist1.get(i-1).getParticipation();
        part.setState(Participation.State.REFUSED);
        participationManager.edit(part);
        notificationManager.remove(partnotlist1.get(i-1));
        return "/user/notification?faces-redirect=true";
    }
    
    public String setConfirmed(){
        Participation part = partnotlist1.get(i-1).getParticipation();
        part.setState(Participation.State.CONFIRMED);
        participationManager.edit(part);
        //FacesContext context = FacesContext.getCurrentInstance(); 
        //context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", "Event added to your calendar"));
        notificationManager.remove(partnotlist1.get(i-1));
        return "/user/notification?faces-redirect=true";
    }
    
    public String moveEvent(){
        event.setData(newdate);
        eventManager.edit(event);
        //FacesContext context = FacesContext.getCurrentInstance(); 
        //context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO ,"", "Event added to your calendar"));
        return "/user/notification?faces-redirect=true";
    }
   
}
